<?php
/*
 * Creative Bulletin Board Generation 3
 * Version 1.0 Beta 1
 +------------------------------------
 * Core Application System / Interface
 * Started: August 30, 2009 08:39 AM
 * Script version: 1.0
 +------------------------------------
 */
interface application_interface{
	public function returninitvars();
	public function construct();
	public function run();
	public function destruct();
}

class application_system{
	public function __construct($interface, $application){
		global $db;
		$this->application_name = str_replace("/", "", $application);
		if (IN_WINDOWS) $this->application_name = str_replace("\\", "", $this->application_name);
		$this->application_name = str_replace("'", "", $this->application_name);
		$this->application_universal_path = realpath(UNIVERSAL_PATH.$this->application_name)."/";
		if(strcmp($interface, "frontend") == 0){
			$query = $db->select("installed_applications", "name", 0, 
			" WHERE `name` = '{$this->application_name}' AND `interface` = 'universal' OR `name` = '{$this->application_name}' AND `interface` = 'frontend'");
			$is_installed = $db->num_rows($query);
			if($is_installed !== 0){
				$this->application_path = realpath(FRONTEND_PATH.$this->application_name)."/";
				$this->application_complete_path = $this->application_path."app.php";
				if(!file_exists($this->application_complete_path)){
					trigger_error("Frontend Application Error: Initializer for Application {$this->application_name} could not be found.");
				}
			}
			else{
				trigger_error("The application requested is not installed or does not exist.");
			}
		}
		elseif(strcmp($interface, "backend") == 0){
			$query = $db->select("installed_applications", "name", 0, 
			" WHERE `name` = '{$this->application_name}' AND `interface` = 'universal' OR `name` = '{$this->application_name}' AND `interface` = 'backend'");
			$is_installed = $db->num_rows($query);
			if($is_installed !== 0){
				$this->application_path = realpath(BACKEND_PATH.$this->application_name)."/";
				$this->application_complete_path = $this->application_path."app.php";
				if(!file_exists($this->application_complete_path)){
					trigger_error("Backend Application Error: Initializer for Application {$this->application_name} could not be found.");
				}
			}
			else{
				trigger_error("The application requested is not installed or does not exist.");
			}
		}
		else{
			trigger_error("Interface {$interface} isn't a valid interface (frontend / backend).");
		}
		require_once($this->application_complete_path);
		if(class_exists("application") !== false && ($this->application = new application) !== false){
			$this->application->construct();
			$this->applicationInfo = $this->application->returninitvars();
			$this->applicationName = $this->applicationInfo["appName"];
			$this->applicationDesc = $this->applicationInfo["appDesc"];
			$this->applicationAuthor = $this->applicationInfo["appAuthor"];
			$this->applicationVersion = $this->applicationInfo["appVersion"];
			$this->applicationCopyright= $this->applicationInfo["copyright"];
			$this->defaultModule = $this->applicationInfo["defaultModule"];
			$this->application->run();
			$this->application->destruct();
		}
		else trigger_error("Couldn't initialize application {$this->application_name}.");
	}
	
	public function getApplicationShortName(){
		return $this->application_name;
	}
	
	public function getApplicationName(){
		return $this->applicationName;
	}
	
	public function getApplicationPath(){
		return $this->application_path;
	}
	
	public function getApplicationDesc(){
		return $this->applicationDesc;
	}
	
	public function getApplicationAuthor(){
		return $this->applicationAuthor;
	}
	
	public function getApplicationCopyright(){
		return $this->applicationCopyright;
	}
	
	public function getApplicationVersion(){
		return $this->applicationVersion;
	}
	
	public function getDefaultModule(){
		return $this->defaultModule;
	}
}
